package com.h.files.selector.activity.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Parcelable;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public final class UiUtils {

	private static Toast toast;

	UiUtils() {
	}

	/**
	 * dp转px
	 * 
	 * @param context
	 * @param val
	 * @return
	 */
	public static int dp2px(Context context, float dpVal) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
	}

	/**
	 * sp转px
	 * 
	 * @param context
	 * @param val
	 * @return
	 */
	public static int sp2px(Context context, float spVal) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, context.getResources().getDisplayMetrics());
	}

	/**
	 * px转dp
	 * 
	 * @param context
	 * @param pxVal
	 * @return
	 */
	public static float px2dp(Context context, float pxVal) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (pxVal / scale);
	}

	/**
	 * px转sp
	 * 
	 * @param fontScale
	 * @param pxVal
	 * @return
	 */
	public static float px2sp(Context context, float pxVal) {
		return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
	}

	/**
	 * 获得屏幕高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		DisplayMetrics outMetrics = getDisplayMetrics(context);
		return outMetrics.widthPixels;
	}

	/**
	 * 得到屏幕宽高
	 * 
	 * @param context
	 * @return
	 */
	public static Point getScreen(Context context) {
		DisplayMetrics outMetrics = getDisplayMetrics(context);
		Point wh = new Point(outMetrics.widthPixels, outMetrics.heightPixels);
		return wh;
	}

	/**
	 * 获得屏幕宽度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		DisplayMetrics outMetrics = getDisplayMetrics(context);
		return outMetrics.heightPixels;
	}

	private static DisplayMetrics getDisplayMetrics(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics;
	}

	/**
	 * 获得状态栏的高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getStatusHeight(Context context) {
		int statusHeight = -1;
		try {
			Class<?> clazz = Class.forName("com.android.internal.R$dimen");
			Object object = clazz.newInstance();
			int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
			statusHeight = context.getResources().getDimensionPixelSize(height);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return statusHeight;
	}

	/**
	 * 获取当前屏幕截图
	 * 
	 * @param activity
	 * @param concatStatusBar
	 *            是否包含状态栏
	 * @return
	 */
	public static Bitmap snapShotWithStatusBar(Activity activity, boolean concatStatusBar) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		int y = 0;
		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		if (!concatStatusBar) {
			Rect frame = new Rect();
			activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
			y = frame.top;
			height -= y;
		}
		bp = Bitmap.createBitmap(bmp, 0, y, width, height);
		view.destroyDrawingCache();
		return bp;
	}

	/**
	 * 创建快捷方式
	 * 
	 * @param cxt
	 *            Context
	 * @param icon
	 *            快捷方式图标
	 * @param title
	 *            快捷方式标题
	 * @param cls
	 *            要启动的类
	 */
	public void createDeskShortCut(Context cxt, int icon, String title, Class<?> cls) {
		// 创建快捷方式的Intent
		Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		// 不允许重复创建
		shortcutIntent.putExtra("duplicate", false);
		// 需要现实的名称
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
		// 快捷图片
		Parcelable ico = Intent.ShortcutIconResource.fromContext(cxt.getApplicationContext(), icon);
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ico);
		Intent intent = new Intent(cxt, cls);
		// 下面两个属性是为了当应用程序卸载时桌面上的快捷方式会删除
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");
		// 点击快捷图片，运行的程序主入口
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		// 发送广播。OK
		cxt.sendBroadcast(shortcutIntent);
	}

	/**
	 * 短时间显示提示
	 * 
	 * @param message
	 */
	public static void showShort(Context context, String message) {
		if (null != toast) {
			toast.cancel();
			toast = null;
		}
		toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	/**
	 * 长时间显示提示
	 * 
	 * @param message
	 */
	public static void showLong(Context context, String message) {
		if (null != toast) {
			toast.cancel();
			toast = null;
		}
		toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

}
